package com.lsu.observer.homework;

import java.util.ArrayList;
import java.util.List;

/**
 * 火情
 *
 * @Author wang suo
 * @Date 2020/10/22 0022 9:00
 * @Version 1.0
 */
public class FireSubject implements Subject {

    private List<Observer> observers;
    private Boolean changed;

    FireSubject() {
        this.observers = new ArrayList<>();
        this.changed = false;
    }

    @Override
    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void deleteObserver(Observer observer) {
        observers.remove(observer);
    }

    /**
     * 监视有没有起火: 如果起火了就调用这个方法
     */
    @Override
    public void notifyObservers() {
        if (changed) {
            for (Observer observer : observers) {
                observer.update();
            }
            System.out.println("通知管理员成功!");
        }
    }

    /**
     * 起火了
     */
    void fire() {
        System.out.println("起火了!");
        this.changed = true;
        notifyObservers();
    }

}
